set more off

use "Data/anes_mergedfile_1992to1997f.dta", clear

* CREATING PLACEMENT KNOWLEDGE SCALE W GSS, GUAR JOB, HEALTH CARE, code from ANES_92_96.do
*job standard of living -does have 1994 questions for party
tokenize I job 1 8 6 I I "V923718 V940930 V960483" "92 94 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  & X>0  \ sum *`2'_Y
*Clinton - job standard of living
tokenize I c_job  0 9 1 I I "V923720 V940931 V960484" "92 94 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  \ sum *`2'_Y
*Bush - job standard of living
tokenize I b_job  0 9 1 I I "V923719 V960485" "92 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  \ sum *`2'_Y
*Democrats - job standard of living
tokenize I d_job  0 9 1 I I "V923722 V940934" "92 94"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  \ sum *`2'_Y
*Republicans - job standard of living
tokenize I r_job  0 9 1 I I "V923721 V940935" "92 94"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  \ sum *`2'_Y

*Government services -can't find in 1993
tokenize I gss 1 8 6 I I "V923701 V940940 V960450" "92 94 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=1-((X-`3')/`5') if X<`4' & X>0
tabulate V923703 V923701, mis
*Clinton - Government services
tokenize I c_gss 0 9 1 I I "V923703 V940941 V960453" "92 94 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'
*Bush-Dole - Government services -
tokenize I b_gss 0 9 1 I I "V923702 V960455" "92 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4' 
*Democrat - Government services -can't find in 1996
tokenize I d_gss 0 9 1 I I "V923705 V940944 V960461" "92 94 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'
*Republican - Government services -can't find in 1996
tokenize I r_gss 0 9 1 I I "V923704 V940945 V960462" "92 94 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'
sum gss*

*Health insurance
tokenize I health 1 8 6 I I "V923716 V940950 V960479" "92 94 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=X if X<`4' & X>0
*Clinton - 
tokenize I c_health 0 9 1 I I " V940951 V960480" " 94 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=X if X<`4'
*Dole
tokenize I b_health 0 9 1 I I "V960481" "96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=X if X<`4'
*Democrat -
tokenize I d_health 0 9 1 I I "V940954" "94"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=X if X<`4'
*Republican -
tokenize I r_health 0 9 1 I I "V940955" "94 "
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=X if X<`4'
sum health*
cor health*94 health*96 *health*94 *health*96


*defense spending
tokenize I def 1 8 6 I I "V923707 V940929 V960463" "92 94 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  & X>0 \ sum *`2'_Y
*Clinton defense spending-no 1994
tokenize I c_def  0 9 1 I I "V923709 V960466" "92 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  \ sum *`2'_Y
*Bush defense spending-no 1994
tokenize I b_def  0 9 1 I I "V923708 V960469" "92 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  & X>0 \ sum *`2'_Y

*ideology
tokenize I ide 1 8 6 I I "V923509 V940839 V960365" "92 94 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  & X>0 \ sum *`2'_Y
tabulate V923509 V923515, mis
*Clinton-ideology
tokenize I c_ide  0 9 1 I I "V923515 V960369" "92 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  \ sum *`2'_Y
*Bush-Dole ideology
tokenize I b_ide  0 9 1 I I "V923514 V960371" "92 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  \ sum *`2'_Y
*Democrat-ideology
tokenize I d_ide  0 9 1 I I "V923518 V960379" "92 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  \ sum *`2'_Y
*Republican-ideology
tokenize I r_ide  0 9 1 I I "V923517 V960380" "92 96"
for any `8' :tab X, mis \ tab X, mis nol
for any `8' \ any `9':g `2'_Y=((X-`3')/`5') if X<`4'  \ sum *`2'_Y


*abortion 
 *no question about parties in 1992 pre
for any V923732 V923733 V923734 V960503    :tab X, mis
  for any V923732 V941014 V960503 \ num 92 94 96:g abr_Y =1-( (X - 1)/3) if X < 5  & X>0
  for any V923734 V960506     \ num 92 96:g c_abr_Y =X
  for any V923733 V960509 \ num 92 96:g b_abr_Y =X 
tab1 abr_* *abr* V923734 V960506
sum abr*
tabulate abr_92 abr_96
recode *_abr_* (7 9 =.)
g kg_abr_92 = c_abr_92 >b_abr_92 & c_abr_92!=8 & b_abr_92!=8 if c_abr_92<. & b_abr_92<. 
g kg_abr_96 = c_abr_96 >b_abr_96 & c_abr_96!=8 & b_abr_96!=8 if c_abr_96<. & b_abr_96<.

*ideology
recode c_ide_* b_ide_* d_ide_* r_ide_* (0 =.)
g kg_ide_92 = c_ide_92 <b_ide_92 & c_ide_92!=8 & b_ide_92!=8 if c_ide_92<.  & b_ide_92<.
g kg_ide_96 = c_ide_96 <b_ide_96 & c_ide_96!=8 & b_ide_96!=8 if c_ide_96<. & b_ide_96<.
g kg_idep_92 = d_ide_92 <r_ide_92 & d_ide_92!=8 & r_ide_92!=8 if d_ide_92<.  & r_ide_92<.
g kg_idep_96 = d_ide_96 <r_ide_96 & d_ide_96!=8 & r_ide_96!=8 if d_ide_96<. & r_ide_96<.
*defense
recode c_def_* b_def_* (0 =.)
g kg_def_92 = c_def_92 <b_def_92 & c_def_92!=8 & b_def_92!=8 if c_def_92<.  & b_def_92<.
g kg_def_96 = c_def_96 <b_def_96 & c_def_96!=8 & b_def_96!=8 if c_def_96<. & b_def_96<.
for any abr ide idep def: corr kg_X_92  kg_X_96 
tab  c_def_92  c_def_96
tab  b_def_92  b_def_96

*Help blacks
tab1 V960490 V960492
g kg_aid_Blacks_96 = V960490 < V960492 &  V960490!=8& V960492!=8 if  V960490<9 & V960492<9
tab1 V940938 V940939
g kg_aid_Blacks_94 = V940938 < V940939 &  V940938!=8& V940939!=8 if  V940938<9 & V940939<9 & V940938>0 & V940939>0
sum kg*
pwcorr kg*

* how much do r's self-placements  on gss move between 92 and 96?
tab gss_92 gss_96, mis
g chgss = abs(gss_96-gss_92)
egen mchgss = mean(abs(gss_96-gss_92))

*health insurance (private health insurance)
sum *health* 
corr *_health* 
g health9496_1_1 = d_health_94 <r_health_94 & c_health_96 <b_health_96 & d_health_94!=8 & r_health_94!=8 & c_health_96!=8 & b_health_96!=8 if d_health_94<. & r_health_94<. & c_health_96<. & b_health_96<.
tabulate  health9496_1_1 
g hlth94 = d_health_94 <r_health_94 & d_health_94!=8 & r_health_94!=8 if d_health_94<. & r_health_94<. 
g hlth96 = c_health_96 <b_health_96 & c_health_96!=8 & b_health_96!=8 if c_health_96<. & b_health_96<.

*government services-knowledge scale
sum *_gss*
tabulate gss_92 gss_96
recode *_gss_* (0 =.)
g gss_1_1 = c_gss_92 >b_gss_92 & c_gss_96 >b_gss_96 & c_gss_92!=8 & c_gss_96!=8 & b_gss_92!=8 & b_gss_96!=8 if c_gss_92<. & c_gss_96<. & b_gss_92<. & b_gss_96<.
tabulate  gss_1_1  , mis
tabulate  gss_1_1 c_gss_92 , mis
g gss9496_1_1 = d_gss_94 >r_gss_94 & c_gss_96 >b_gss_96 & d_gss_94!=8 & r_gss_94!=8 & c_gss_96!=8 & b_gss_96!=8 if d_gss_94<. & r_gss_94<. & c_gss_96<. & b_gss_96<.
tab gss9496_1_1
g gss92 = c_gss_92 >b_gss_92 & c_gss_92!=8 & b_gss_92!=8 if c_gss_92<. & b_gss_92<. 
g gss94 = d_gss_94 >r_gss_94 & d_gss_94!=8 & r_gss_94!=8 if d_gss_94<. & r_gss_94<.
g gss96 = c_gss_96 >b_gss_96 & c_gss_96!=8 & b_gss_96!=8 if c_gss_96<. & b_gss_96<.


*jobs-knowledge scale
tabulate b_job_92 , mis
tabulate c_job_92 , mis
recode *_job_* (0 =.)
g job_1_1 = c_job_92 <b_job_92 & c_job_96 <b_job_96 & c_job_92!=8 & c_job_96!=8 & b_job_92!=8 & b_job_96!=8 if c_job_92<. & c_job_96<. & b_job_92<. & b_job_96<.
tabulate  job_1_1  , mis
tabulate  job_1_1 c_job_92 , mis
tabulate  job_1_1 b_job_96 , mis
g job92 = c_job_92 <b_job_92 & c_job_92!=8 & b_job_92!=8 if c_job_92<. & b_job_92<.
g job94 = d_job_94 <r_job_94 & d_job_94!=8 & r_job_94!=8 if d_job_94<. & r_job_94<.
g job96 = c_job_96 <b_job_96 & c_job_96!=8 & b_job_96!=8 if c_job_96<. & b_job_96<.

g job9496_1_1 = d_job_94 <r_job_94 & c_job_96 <b_job_96 & d_job_94!=8 & r_job_94!=8 & c_job_96!=8 & b_job_96!=8 if d_job_94<. & r_job_94<. & c_job_96<. & b_job_96<.
tab job9496_1_1 

egen plc9296_e = rowmean(job92 job94 job96 gss92 gss94 gss96 hlth94 hlth96)
tab plc9296_e, mis
g pk9296_e = 1 if plc9296_e <=0.2
replace pk9296_e = 2 if (plc9296_e > 0.2 & plc9296_e<=0.4)
replace pk9296_e = 3 if plc9296_e > 0.4 & plc9296_e<=0.6
replace pk9296_e = 4 if plc9296_e > 0.6 & plc9296_e<=0.8
replace pk9296_e = 5 if plc9296_e > 0.8 & plc9296_e<.


egen plc9296_h = rowmean(health9496_1_1 gss_1_1 gss9496_1_1 job_1_1 job9496_1_1)
tab plc9296_h, mis
g pk9296_h = 1 if plc9296_h <=0.2
replace pk9296_h = 2 if (plc9296_h > 0.2 & plc9296_h<=0.4)
replace pk9296_h = 3 if plc9296_h > 0.4 & plc9296_h<=0.6
replace pk9296_h = 4 if plc9296_h > 0.6 & plc9296_h<=0.8
replace pk9296_h = 5 if plc9296_h > 0.8 & plc9296_h<.


* CREATING (28 ITEM) GLOBAL KNOWLEDGE SCALE, GENERAL KNOWLEDGE PLUS PLACEMENTS, W IMPUTED VALUES
********General Knowledge Scale***************
g kg_congresspost3=V925919==1 if V925919!=0 & V925919!=.
g kg_generalpost1=V925916==1 if V925916!=0 & V925916!=.
g kg_generalpost2=V925917==1 if V925917!=0 & V925917!=.
g kg_generalpost3=V925920==3 if V925920!=0 & V925920!=.
g kg_generalpost4=V925921==1 if V925921!=0 & V925921!=.
*g kg_generalpost5=V925915==5 if V925915!=0 & V925915!=. //Which party is more conservative
g kg_foreignpost1=V925918==1 if V925918!=0 & V925918!=.
g kg_congress1=V961192==1 if V961192!=0 & V961192!=.
g kg_general1=V961189==1 if V961189!=0 & V961189!=.
g kg_general2=V961190==1 if V961190!=0 & V961190!=.
g kg_foreign1=V961191==1 if V961191!=0 & V961191!=.
g krg_rating92pre=5-V924205 if V924205!=9 & V924205!=.
g krg_rating92post=5-V926250 if V926250!=9 & V926250!=0 
g krg_rating94post=5-V941438 if V941438!=9 & V941438!=0 
g krg_rating96pre=5-V960070 if V960070!=9 & V960070!=.
g krg_rating96post=5-V960940 if V960940!=9 & V960940!=0 
egen kg_rating=rowmean(krg_rating*)
*1994
tab1 V941006-V941013
recode V941006 1 = 1 5 8 = 0 9 =.,g(kg_Gore_1994)
recode V941007 1 = 1 5 8 = 0 9 =.,g(kg_Rehnquist_1994)
recode V941008 1 = 1 5 8 = 0 9 =.,g(kg_Yeltsin_1994)
recode V941009 1 = 1 5 8 = 0 9 =.,g(kg_foley_1994)
recode V941010 3 = 1 1 2 8 = 0 9 =.,g(kg_Constitution_1994)
recode V941011 1 = 1 2 3 8 = 0 9 =.,g(kg_Judge_1994)
recode V941012 5 = 1 1 8 = 0 9 =.,g(kg_house_1994)
recode V941013 5 = 1 1 8 = 0 9 =.,g(kg_Senate_1994)
g kg_job92 = c_job_92 <b_job_92 & c_job_92!=8 & b_job_92!=8 if c_job_92<. & b_job_92<.
g kg_job94 = d_job_94 <r_job_94 & d_job_94!=8 & r_job_94!=8 if d_job_94<. & r_job_94<.
g kg_job96 = c_job_96 <b_job_96 & c_job_96!=8 & b_job_96!=8 if c_job_96<. & b_job_96<.

g kg_gss92 = c_gss_92 >b_gss_92 & c_gss_92!=8 & b_gss_92!=8 if c_gss_92<. & b_gss_92<. 
g kg_gss94 = d_gss_94 >r_gss_94 & d_gss_94!=8 & r_gss_94!=8 if d_gss_94<. & r_gss_94<.
g kg_gss96 = c_gss_96 >b_gss_96 & c_gss_96!=8 & b_gss_96!=8 if c_gss_96<. & b_gss_96<.
g kg_hlth94 = d_health_94 <r_health_94 & d_health_94!=8 & r_health_94!=8 if d_health_94<. & r_health_94<. 
g kg_hlth96 = c_health_96 <b_health_96 & c_health_96!=8 & b_health_96!=8 if c_health_96<. & b_health_96<.

 
pwcorr kg*

tab1 V940214 V940218 V940222 V960261 V960265 V960269 V961010 V961014 V961018
recode V940214 V940218 V960261 V960265 V961010 V961014 (1=1) (0=.) (2/9=0), prefix("kg_") 
sum kg_*
*pwcorr kg* ,obs



sum kg*

for var kg*: impute X  kg_ide_92 kg_ide_96   kg_rating  ,g(iX) \ replace X=iX
alpha kg* , 

alpha kg* , item

egen kg_total=rowtotal(kg_*)
tab kg_total

*replace kg_total =(kg_total)/62

set more off



/*
*calculated below so that we can get the deciles more even based on missingness induced by the scale
g kgdec = 1 if kg_total <= 0.1
replace kgdec = 2 if (kg_total > 0.1 & kg_total <= 0.2)
replace kgdec = 3 if (kg_total > 0.2 & kg_total <= 0.3)
replace kgdec = 4 if (kg_total > 0.3 & kg_total <= 0.4)
replace kgdec = 5 if (kg_total > 0.4 & kg_total <= 0.5)
replace kgdec = 6 if (kg_total > 0.5 & kg_total <= 0.6)
replace kgdec = 7 if (kg_total > 0.6 & kg_total <= 0.7)
replace kgdec = 8 if (kg_total > 0.7 & kg_total <= 0.8)
replace kgdec = 9 if (kg_total > 0.8 & kg_total <= 0.9)
replace kgdec = 10 if (kg_total > 0.9 & kg_total<.)
*/


* 92-96 Economic Items in ARS Econ Items Scale

* Recoding 1992 Economic Items
set more off

for any 923725 923726 923727 923728 923730 923811 923813 923815 ///
	923817 923818: recode VX 7 = 3 8 = . 9 = .

for any 925729 925731 925730: recode VX 8 = . 9 = .

for any 923701 923716 923718 923725 923726 923727 923728 923730 925729 925730 925731 926024 ///
	926025 926026 926027 926028 926029: recode VX 0 = . 8 = . 9 = .

for any 925316 925318 925320: recode VX 888 = . 997 = . 998 = . 999 = .

* checking code:

for any 923701 923716 923718 923725 923726 923727 923728 923730 923811 923813 ///
	923815 923817 923818 925316 925318 925320 925729 925730 925731 926024 ///
	926025 926026 926027 926028 926029 923725 923726 923727 923728 923730 ///
	923813 923817 923818: tab VX

* Recoding 1996 Economic items

for any V960496 V960497 V960498 V960500 V960501 V960560 V960561 ///
	V960562 V960564 V960565: recode X 2 = 5 7 = 5 0 =. 8= . 9 = .

for any V960450 V960479 V960483 V960496 V960497 V960498 V960500 V960501 V960560 V960561 ///
	V960562 V960564 V960565 V961229 V961230 V961231 V961232 ///
	V961233 V961234 V961144 V961145 V961146: recode X 0 = . 8 = . 9 = . 

for any V961033 V961035 V961036: recode X 996 = . 997 = . 998 = . 999 = .

* checking code:

for any V960496 V960497 V960498 V960500 V960501 V960560 V960561 ///
	V960562 V960564 V960565 V960450 V960479 V960483 V960496 V960497 ///
	V960498 V960500 V960501 V960560 V960561 V960562 V960564 V960565 ///
	V961229 V961230 V961231 V961232 V961233 V961234 V961144 V961145 ///
	V961146 V961033 V961035 V961036: tab X


* count total answers out of possible 25 for each R

egen nms_92 = rownonmiss (V923701 V923716 V923718 V923725 V923726 V923727 V923728 V923730 V923811 V923813 ///
	V923815 V923817 V923818 V925316 V925318 V925320 V925729 V925730 V925731 V926024 ///
	V926025 V926026 V926027 V926028 V926029)

egen nms_96 = rownonmiss (V960450 V960479 V960483 V960496 ///
	V960497 V960498 V960500 V960501 V960560 V960561 V960562 V960564 V960565 ///
	V961033 V961035 V961036 V961229 V961230 V961231 V961232 V961233 V961234 ///
	V961144 V961145 V961146)


* drop R's with valid answers for < 75% of items
tab nms_96  nms_92 if  nms_92!= 0 & nms_96!=0   // 597- 534 = 63, about 10% of the sample is lost 
drop if nms_92 < 19
drop if nms_96 < 19
tab nms_96  nms_92 if  nms_92!= 0 & nms_96!=0   // 597- 534 = 63, about 10% of the sample is lost 

* IMPUTE answers for remaining R's with missing data

* first code demographics
tokenize age 1203
tab V94`2', mis
g age =V94`2'
replace age=0 if V94`2' ==.
g m_age= V94`2' ==.
g age2 = sqrt( age)
tokenize edu 1209
tab V94`2', mis
recode  V94`2' 99 =.
g `1' =V94`2'
g m_`1' =V94`2' ==.
replace `1'=0 if V94`2'==.
tokenize female 1434
tab V94`2', mis
*tab V94`2', mis nol
g `1' =V94`2' == 2
tokenize black 1435
tab V94`2', mis
tab V94`2', mis nol
recode  V94`2' 9 =.
g `1' =V94`2' == 2
g m_`1' =V94`2' ==.
replace `1'=0 if V94`2'==.
tokenize union 1401
tab V94`2', mis
tab V94`2', mis nol
g `1' =V94`2' == 1
g m_`1' =V94`2' ==.
replace `1'=0 if V94`2'==.
tokenize income 1404
tab V94`2', mis
tab V94`2', mis nol
recode  V94`2' 99 =.
g `1' =V94`2'
g m_`1' =V94`2' ==.
replace `1'=0 if V94`2'==.
tokenize children 1429 1431
tab V94`2' V94`3', mis
recode  V94`2'   V94`3' (9 =.) (0=.)
egen `1' = rsum( V94`2' V94`3')
g m_`1' =V94`2'==. & V94`3'==.
replace `1'=0 if `1' ==.
sum children 
tokenize professional 1223
tab V94`2', mis
tab V94`2', mis nol
g `1' =V94`2' == 1 | V94`2' == 2
tokenize clerical 1223
g `1' =V94`2' == 5 
tokenize House_wife 1215
tab V94`2', mis
tab V94`2', mis nol
g `1' =V94`2' == 17 | V94`2' == 70
tokenize unemployed 1215
tab V94`2', mis
tab V94`2', mis nol
g `1' =V94`2' == 40
tokenize Protestant 1105
tab V94`2', mis
tab V94`2', mis nol
g `1' =V94`2' == 1
tokenize Catholic `2'
g `1' =V94`2' ==2
tokenize Jewish `2'
g `1' =V94`2' == 3
tokenize church 1103
tab V94`2', mis
tab V94`2', mis nol
g `1' = 1-((V94`2' -1)/5)
g m_`1' =V94`2' ==.
replace `1'=0 if V94`2'==.
tokenize south 0011
tab V94`2', mis
tab V94`2', mis nol
g `1' = V94`2' == 3
tokenize East 0011
g `1' = V94`2' == 1
tokenize West 0011
g `1' = V94`2' == 4


*check variables before imputation
tab1  V923701 V923716 V923718 V923725 V923726 V923727 V923728 V923730 V923811 V923813 ///
	V923815 V923817 V923818 V925316 V925318 V925320 V925729 V925730 V925731 V926024 ///
	V926025 V926026 V926027 V926028 V926029

tab1   V960450 V960479 V960483 V960496 ///
	V960497 V960498 V960500 V960501 V960560 V960561 V960562 V960564 V960565 ///
	V961033 V961035 V961036 V961229 V961230 V961231 V961232 V961233 V961234 ///
	V961144 V961145 V961146

*then do imputation:
for var V923701 V923716 V923718 V923725 V923726 V923727 V923728 V923730 V923811 V923813 ///
	V923815 V923817 V923818 V925316 V925318 V925320 V925729 V925730 V925731 V926024 ///
	V926025 V926026 V926027 V926028 V926029: impute X age- West, g(iX)
for var V960450 V960479 V960483 V960496 ///
	V960497 V960498 V960500 V960501 V960560 V960561 V960562 V960564 V960565 ///
	V961033 V961035 V961036 V961229 V961230 V961231 V961232 V961233 V961234 ///
	V961144 V961145 V961146: impute X age- West, g(iX)

* standardizing all variables

for var V923701 V923716 V923718 V923725 V923726 V923727 V923728 V923730 V923811 V923813 ///
	V923815 V923817 V923818 V925316 V925318 V925320 V925729 V925730 V925731 V926024 ///
	V926025 V926026 V926027 V926028 V926029 ///
	V960450 V960479 V960483 V960496 V960497 V960498 V960500 V960501 V960560 V960561 ///
	V960562 V960564 V960565 V961033 V961035 V961036 V961229 V961230 V961231 V961232 ///
	V961233 V961234 V961144 V961145 V961146: egen sX = std(iX) \ replace iX = sX

* reversing scales that run from higher values=liberal to smaller values=conservative

for any iV923701 iV925316 iV925318 iV925320 iV925729 iV925731 iV926025 iV926026 iV926027  ///
iV960450 iV961033 iV961035 iV961036 iV961230 iV961232 iV961233 iV961144 iV961146: replace X=X*-1


** ARS 25 ITEM "ECONOMIC POLICY" SCALES (Note: The questions in the below scales have been placed in the same order in 92 & 96)

* 92 create 25 item economic policy scale
alpha iV923701 iV923716 iV923718 iV923725 iV923726 iV923727 iV923728 iV923730 iV923811 iV923813 ///
	iV923815 iV923817 iV923818 iV925316 iV925318 iV925320 iV925729 iV925730 iV925731 iV926024 ///
	iV926025 iV926026 iV926027 iV926028 iV926029, g(econ25_92) std item 

* 96 create 25 item economic policy scale
alpha iV960450 iV960479 iV960483 iV960496 iV960497 iV960498 iV960500 iV960501 iV960560 iV960564  ///
	iV960561 iV960565 iV960562 iV961033 iV961036 iV961035 iV961144 iV961145 iV961146 iV961229 ///
	iV961230 iV961232 iV961233 iV961234 iV961231, g(econ25_96) std item
 

** 16 ITEM ECONOMIC POLICY SCALES (EXCLUDING FEELING THERMS AND EQUALITY ITEMS)

* 92 16 item economic policy scale
/* alpha iV923701 iV923716 iV923718 iV923725 iV923726 iV923727 ///
	iV923728 iV923730 iV923811 iV923813 iV923815 iV923817 ///
	iV923818 iV925729 iV925730 iV925731, g(econ16_92) std item 
	
* 96 16 item economic policy scale
alpha iV960450 iV960479 iV960483 iV960496 iV960497 iV960498 ///
	iV960500 iV960501 iV960560 iV960561 iV960562 iV960564 ///
	iV960565 iV961144 iV961145 iV961146, g(econ16_96) std item
*/
	
*check for missing values
sum iV*

*check correlations
corr iV92*
corr iV96*

*set variables 
global issues92 iV923701 iV923716 iV923718 iV923725 iV923726 iV923727 iV923728 iV923730 iV923811 iV923813 ///
	iV923815 iV923817 iV923818 iV925316 iV925318 iV925320 V925729 iV925730 iV925731 iV926024 ///
	iV926025 iV926026 iV926027 iV926028 iV926029
global issues96 iV960450 iV960479 iV960483 iV960496 iV960497 iV960498 iV960500 iV960501 iV960560 iV960564  ///
	iV960561 iV960565 iV960562 iV961033 iV961036 iV961035 iV961144 iV961145 iV961146 iV961229 ///
	iV961230 iV961232 iV961233 iV961234 iV961231
tab V926025 V961230

*create
xtile kgdec =kg_total if iV960450!=.,n(5) 
*kdensity kg_total 
tab kgdec 
	
capture drop scale_92   //  in case something goes wrong with the loop
capture drop scale_96   //  ditto
tempname memhold
postfile `memhold' kgdec ni corr N  using Data/temporary/ARS_figure_1_25_kg5, replace	
forvalues kgdec=1/5 {
	forvalues i=2/24 {                       // loop over items, 
		local c = comb(25,`i')               // number of possible combinations
		local d = `c'					     // number of random draws we will do for each scale length - set to number of combinations but lowered to a threshold on the next line
		if `c' >500  local d =500              // do no more than ...
		forvalues j=1/`d' {                  // loop where we randomly pick issues for each number of items in the scale I
			dis "kgdec:`kgdec' ni: `i', `j' of `d'"
			global s1 ""
			global s2 ""
				forvalues k=1/`i' {          // loop to fill $s with a random set of the issues
					local t = 0
					while (`t' ==0) {		 // loop to make sure that r has not been previously drawn		
 					   local t = 1
					   local r=1+int(25*runiform())  //  randomly draw one of the 25 variables
					   local r`k' = `r'				 //  store r 
					   local h =`k' -1
					   forvalues q = 1/`h' {          // check to make sure r has not been drawn
							if `r' == `r`q'' & `k'!= 1 local t = 0
					   }
					}
					tokenize $issues92               // assigned variables to numbers for 1992
					*disp "`r'   ``r''   "
					global s1 $s1 ``r''        //  add the randomly drawn issue to the $s
					tokenize $issues96               // assigned variables to numbers for 1996
					global s2 $s2 ``r''        //  add the randomly drawn issue to the $s
					*disp "`r'   ``r''   "

				}
			display "$s1"
			display "$s2"
			egen scale_92 = rowmean($s1)
			egen scale_96 = rowmean($s2)
			quietly corr scale_92 scale_96 if kgdec==`kgdec'	
			if _rc==0 {
				 post `memhold' (`kgdec') (`i') (r(rho) ) (r(N) ) 
			   }
			drop scale_92 scale_96
		}
		
	}
		
}


forvalues kgdec=1/5 {        // single item scales
	forvalues i=1/25 {                    
		tokenize $issues92              
		global s1  ``i''        
		tokenize $issues96               
		global s2  ``i''        
		quietly corr $s1 $s2 if kgdec==`kgdec'
		if _rc==0 {
			post `memhold' (`kgdec') (1) (r(rho) ) (r(N) ) 
		}
			
			
	}
}

forvalues kgdec=1/5 {           //25 item scale
	corr econ25_92 econ25_96 if kgdec==`kgdec'
			if _rc==0 {
			post `memhold' (`kgdec') (25) (r(rho) ) (r(N) ) 
			}
}
postclose `memhold'                  //  this closes ARS_figure_1 -- if the loop crashes, you have to run this on its own

